cmake_minimum_required(VERSION 3.1...3.21)

if(NOT DEFINED OSNAME)
set(OSNAME AstralOS)
endif()

set(CMAKE_SYSTEM_NAME Generic)

# reading config and set config macro

if(${ARCH} MATCHES "arm")
    message(STATUS "using arm v7 toolchain")
    include(${CMAKE_CURRENT_LIST_DIR}/tchain/cmake/arm_none.cmake)
elseif(${ARCH} MATCHES "riscv")
    message(STATUS "using riscv toolchain")
    include(${CMAKE_CURRENT_LIST_DIR}/tchain/cmake/rv64.cmake)
else()
    message(FATAL_ERROR "must define compile toolchain")
endif()



## this is generated by make comand make config
include(${CMAKE_BINARY_DIR}/config.cmake)
configure_file( 
    ${CMAKE_CURRENT_LIST_DIR}/tchain/cmake/GraphViz.cmake
    ${CMAKE_BINARY_DIR}/CMakeGraphVizOptions.cmake
    COPYONLY
)
include(${CMAKE_CURRENT_LIST_DIR}/tchain/cmake/functions.cmake)


## add signoff information
add_definitions(-DASSIGNOFF="${SIGNOFF}")



###################################################################################
##$$$$$$$$$$$$$$$$$$$$$$$ start our project and compiling $$$$$$$$$$$$$$$$$$$$$$$##
###################################################################################
project(${OSNAME} VERSION 1.0 LANGUAGES C ASM)


set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})


include_directories(${CMAKE_CURRENT_LIST_DIR}/comn/Queue)
include_directories(${CMAKE_CURRENT_LIST_DIR}/comn/Type)
include_directories(${CMAKE_CURRENT_LIST_DIR}/comn/Bitmap)
include_directories(${CMAKE_CURRENT_LIST_DIR}/comn/String)
include_directories(${CMAKE_CURRENT_LIST_DIR}/comn/Buffer)
include_directories(${CMAKE_CURRENT_LIST_DIR}/comn/Map)

set(REFERENCE ${CMAKE_CURRENT_LIST_DIR}/reference)  # set referenced 3rd projects
set(GLB_INC_DIR ${CMAKE_CURRENT_LIST_DIR}/include)  # global include path


add_subdirectory(comn)
add_subdirectory(kernel)
add_subdirectory(kservice)
add_subdirectory(driver)
add_subdirectory(processor)




message(STATUS "==========================")
message(STATUS "==========================")
message(STATUS "Building ${OSNAME}")
message(STATUS "==========================")
message(STATUS "==========================")

list(APPEND KERN_LIBS comn kservice arch kernel driver)


##
## build operating system elf
##
# 1. prepare the content of the generated file
# 2. generate a null.c to add_executable
# 3. add it to the executable
set(NULL_COMMENT "//auto generated by cmake")
file(WRITE ${CMAKE_BINARY_DIR}/centry.c ${NULL_COMMENT})
add_executable(${OSNAME} ${CMAKE_BINARY_DIR}/centry.c)
add_dependencies(${OSNAME} ${KERN_LIBS})

##
## transfer libraries to link flags
##
message(STATUS "the result ${KERN_LIBS}")
list(TRANSFORM KERN_LIBS PREPEND -l)

##
## set link flags, loop libraries search, link script and so on
##
set(LINK_OP ${LINK_OP} -T ${ARCH_LD} -L.  -L${CMAKE_BINARY_DIR}/output)
set(LINK_OP ${LINK_OP} -Wl,--start-group ${KERN_LIBS} -Wl,--end-group)
target_link_options(${OSNAME} PUBLIC -O0  ${LINK_OP})